
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/
	
#include "GRTGeoSphere.h"

//contains the functions to render primitives
#include "GRTGLVisu.h"

#include "RTMatrix44.h"
//#include <QGLWidget>
#include <QtOpenGL\qgl.h>

//to get access to the camera position
#include "GRTScene.h"

#include <math.h>
#include "GRTMemoryLeak.h"
//constructor
GRTGeoSphere::GRTGeoSphere():GRTGeometry(GT_SPHERE), _radius(1)
{
	_rotationMarkRadius = 1.5f;
	_translateMarkOffset = 0.2f;
	_translateMark = RTVector3f(0, 0, 0);
	_translateMark.y = _radius + _translateMarkOffset;
}

//destructor
GRTGeoSphere::~GRTGeoSphere(){}

//render the object
void GRTGeoSphere::Render()
{
	glColor3f(0, 1, 0);
	VISU.drawSphere(_radius);
}

//post render function
void GRTGeoSphere::postRender()
{
	//if the sphere is selected, draw the surrounding box and the translation mark
	if(_selected)
	{
		VISU.drawSelectionBox(_radius*2, _radius*2, _radius*2);
	}

	glPopMatrix();
}

//determine if the point v is a point of the geometry object
bool GRTGeoSphere::isGRTGeometry(const RTVector3f& v)const
{
	//calculate the world matrix of the primitive
	RTMatrix44 t;
	t.createTranslation(_translate);

	RTMatrix44 rX;
	rX.createRotationX(_rotate.x * 3.14f / 180.f);
	RTMatrix44 rY;
	rY.createRotationY(_rotate.y * 3.14f / 180.f);
	RTMatrix44 rZ;
	rZ.createRotationZ(_rotate.z * 3.14f / 180.f);

	RTMatrix44 s;
	s.createScale(_scale);

	RTMatrix44 transform = s * rZ * rY * rX * t;

	//get V in local coordinate of the sphere
	RTVector3f localV = v * transform.inverse();
	
	//get the distance between localV and the center of the sphere (0, 0, 0).
	float length = localV.norme();

	//the error offset
	const float error = 0.3f;

	//if the point is inside the sphere
	if (length <= _radius + error && length >= _radius - error)
		return true;

	//the point is not the sphere
	return false;
	
}


//set the sphere radius
void GRTGeoSphere::setRadius(float r)
{
	_radius = r;
	_rotationMarkRadius = r + .5f;
	_translateMark.y = _radius + _translateMarkOffset;
}

//return the sphere radius
float GRTGeoSphere::getRadius()const
{
	return _radius;
}